/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.fedoraaccess.metadataUpdate.datastream;

import cz.mzk.kramerius.commons.DatastreamId;
import cz.mzk.kramerius.commons.Model;
import cz.mzk.kramerius.fedoraaccess.FedoraConnector;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class RelsExtModelUpdater extends DatastreamUpdater {

    private static final String relationNsPrefix = "fedora-model";
    //private static final String relationNsPrefix = "kramerius";
    private static final String relationship = "hasModel";
    private final Model model;

    public RelsExtModelUpdater(FedoraConnector con, Model model) {
        super(con, DatastreamId.RELS_EXT);
        this.model = model;
    }

    @Override
    public void updateDatastream(String pid) throws Exception {
        boolean removed = con.removeRelationships(pid, relationship, relationNsPrefix);
        if (!removed) {
            throw new Exception("failed to remove relationships " + relationNsPrefix + ":" + relationship + " of subject " + pid);
        }
        String modelPid = "model:" + model.toString();
        boolean added = con.addRelationship(pid, relationship, relationNsPrefix, modelPid);
        if (!added) {
            throw new Exception("failed to add relationship " + relationNsPrefix + ":" + relationship + " of subject " + pid + " and object " + modelPid);
        }
    }
}
